//
//  DDChooseJobCategoryViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/23.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDChooseJobCategoryViewController.h"
#import "DDJobCategoryDetailViewController.h"
#import "DDJobCategoryService.h"
#import "DDJobCategoryCell.h"
#import "DDJobCategoryModel.h"
@interface DDChooseJobCategoryViewController ()

@property (strong, nonatomic) DDJobCategoryService *jobService;

@property (strong, nonatomic) NSMutableArray *jobCategoryFirstArray;
@property (strong, nonatomic) NSMutableArray *jobCategorySecondTitleArray;

@property (strong, nonatomic) NSMutableArray *cellHeightArray;

@end

@implementation DDChooseJobCategoryViewController

#pragma mark
#pragma mark - View LifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.jobService = [[DDJobCategoryService alloc] init];
    self.jobCategoryFirstArray = [@[] mutableCopy];
    self.jobCategorySecondTitleArray = [@[] mutableCopy];
    self.cellHeightArray = [@[] mutableCopy];
    
    [self setupData];
    [self setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark
#pragma mark - Private Methods

- (void)setupData
{
    [self requestJobCategoryInfo];
}

- (void)setupView
{
    
}

- (void)requestJobCategoryInfo
{
    WEAKSELF
    [self.jobService queryJobCategoryInfoSuccessBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            DDJobCategoryModel *jobCategory = [DDJobCategoryModel yy_modelWithJSON:responseObject];
            
            [weakSelf.jobCategoryFirstArray addObjectsFromArray:jobCategory.data];
            
            [weakSelf makeJobSecondCategoryTitle:weakSelf.jobCategoryFirstArray];

        } else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
      
        [weakSelf requestFaild];
    }];
}

- (void)makeJobSecondCategoryTitle:(NSMutableArray *)firstArray
{
    [self.cellHeightArray removeAllObjects];
    [self.jobCategorySecondTitleArray removeAllObjects];
    
    for (int i = 0; i < [firstArray count]; i ++) {
        DDJobCategoryFirst *firstCategory = (DDJobCategoryFirst *)firstArray[i];
        NSMutableArray *twoArray = [NSMutableArray array];
        twoArray = [NSMutableArray arrayWithArray:firstCategory.level2];
        NSMutableString *tempString = [[NSMutableString alloc] init];
       
        for (int j = 0; j < [twoArray count]; j ++) {
            DDJobCategorySecond *secondCategory = (DDJobCategorySecond *)twoArray[j];
            
            if (j == [twoArray count] - 1) {
                [tempString appendString:secondCategory.level2_name];
            } else if (j == 0){
                [tempString appendString:[NSString stringWithFormat:@"%@ |",secondCategory.level2_name]];
            } else {
                [tempString appendString:[NSString stringWithFormat:@" %@ |",secondCategory.level2_name]];
            }
        }
        
        CGSize size = CGSizeMake(kScreenWidth - 30, MAXFLOAT);
        UIFont *font = [UIFont systemFontOfSize:13];
        CGSize newSize = [DDUtils boundingRectWithSize:size font:font text: tempString];

        NSInteger height = ceil(newSize.height) + 45;
        
        [self.cellHeightArray addObject:[NSNumber numberWithInteger:height]];
        
        [self.jobCategorySecondTitleArray addObject:tempString];
    }
    
    [self.tableView reloadData];
}

#pragma mark
#pragma mark - CustomDelegate

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.jobCategoryFirstArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"DDJobCategoryCell";
    
    DDJobCategoryCell *cell = (DDJobCategoryCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DDJobCategoryCell" owner:self options:nil] lastObject];
    }
    
    if (indexPath.row < self.jobCategoryFirstArray.count) {
        DDJobCategoryFirst *job = (DDJobCategoryFirst *)self.jobCategoryFirstArray[indexPath.row];
        
        cell.jobTitleLabel.text = job.level1_name;
        if (indexPath.row < [self.jobCategorySecondTitleArray count]) {
            cell.jobSubtitleLabel.text = self.jobCategorySecondTitleArray[indexPath.row];
        }
    }
    
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DDJobCategoryFirst *job = (DDJobCategoryFirst *)self.jobCategoryFirstArray[indexPath.row];
    
    DDJobCategoryDetailViewController *workVC = [[DDJobCategoryDetailViewController alloc] init];
    workVC.title = job.level1_name;
    workVC.jobCategoryArray = [NSMutableArray arrayWithArray:job.level2];
    [self.navigationController pushViewController:workVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.cellHeightArray count] == 0) {
        return 80;
    } else {
        if ([self.cellHeightArray count] > indexPath.row) {
            return [self.cellHeightArray[indexPath.row] floatValue];
        }
        return 80;
    }
}

#pragma mark
#pragma mark - Getters and Setters

@end
